package cn.jekun.shop.app.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.spring.SpringUtils;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpCoupon;
import cn.jekun.component.core.bean.gen.TsAccount;
import cn.jekun.component.core.bean.gen.TsOperationCode;
import cn.jekun.component.core.service.AccountPayService;
import cn.jekun.component.core.service.gen.ErpCouponService;
import cn.jekun.component.pay.common.utils.WebPayTools;
import cn.jekun.component.pay.common.utils.WechatPayConfig;
import cn.jekun.component.pay.common.utils.WechatPayTools;
import cn.jekun.component.pay.webpay.WechatAppPay;
import cn.jekun.shop.common.service.AccountService;
import cn.jekun.shop.web.bean.ShopAccounts;
import cn.jekun.component.pay.wechat.tools.RandomStringGenerator;

/**
 * 分享
 * 
 *
 */
@SuppressWarnings("serial")
public class ShareAction extends AppBaseAction {

	//记录日志信息
	private static Logger logger=Logger.getLogger(WechatAppPay.class);
		
	public String wxConfig() {

		JSONObject jsonObject = new JSONObject();
		try {

			logger.info("********************开始初始化微信config接口***********************");
			WechatPayConfig wechatPayConfig = SpringUtils
					.getBean(WechatPayConfig.BEAN_NAME);

			wechatPayConfig.init();

			String config_signature = "";

			String noncestr = RandomStringGenerator.getRandomStringByLength(32);// 随机字符串

			String timestamp = WechatPayTools.getTimeStamp();
			// 获取token
			String access_token = WechatPayTools.getAccessToken(
					wechatPayConfig.getWechatAppAppId(),
					wechatPayConfig.getWechatAppAppSecret());

			// 根据token获取ticket
			String jsapiTicket = WechatPayTools.getJsapiTicket(access_token);

			// JSAPI初始化需要的签名
			config_signature = "jsapi_ticket=" + jsapiTicket + "&noncestr="
					+ noncestr + "&timestamp=" + timestamp + "&url="
					+ request.getParameter("url");

			config_signature = WebPayTools.getSha1(config_signature);

			jsonObject.put("appId", wechatPayConfig.getWechatAppAppId());

			jsonObject.put("timestamp", timestamp);

			jsonObject.put("nonceStr", noncestr);

			jsonObject.put("signature", config_signature);

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
			
			logger.info("********************初始化微信config接口成功***********************");

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			logger.info("********************初始化微信config接口失败***********************");

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}

		return this.writeJson(jsonObject);

	}

}
